<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Log;
// 应用公共文件
function checkrequestdate($data){
	if(empty($data)){
		return array('status'=>'ok','msg'=>'');
	}
	foreach ($data as $k => $v) {
		if($v == '' || is_null($v)){
			return array('status'=>'false','msg'=>'miss param'.$k);
		}
	}
	return array('status'=>'ok','msg'=>'the param all right');
}

function set_log($log){
	$path = LOG_PATH.date('Y-m-d').'workerman.log';
	$log = "*************".date('Y-m-d H:i:s')."********************\r\n".is_array($log)?serialize($log):$log."\r\n";
	file_put_contents($path,$log,FILE_APPEND);
}

function sendmsgToDevice($device_id,$content){
	//('log')->save(array('info'=>$device_id.'/'.$content,'time'=>date('Y-m-d H:i:s')));
	// dump($device_id);
	// dump($content);
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	$con=socket_connect($socket,'127.0.0.1',8702);
	if(!$con){socket_close($socket);exit;}
	//echo "Link\n";
    $hear=socket_read($socket,1024);
    //echo $hear;  
    if(substr($hear,0,2) == 'ok'){
    	$key = 'pCKvp5hRNm7HGbcucBjCuwmYSZ53UyM8';
    	$str = "HLG*$device_id*0013*MONITOR,$content";
    	// $md5 = md5($str.$key);
    	$md5 = '';
    	$send_str = '['.$md5.$str.']';
    	Log::record($send_str,'error');
		$bool=socket_send($socket, $send_str, strlen($send_str), MSG_EOR);
		//dump($bool);
    }
    
	socket_close($socket);
}

function changeonegpsnew($out){

		
			$turn = $out['longitude'].','.$out['latitude'];
			
			// 高德地图的
			$key = 'ae27f336dfae44aee280b57bf3350099';
			$url = "http://restapi.amap.com/v3/assistant/coordinate/convert?coordsys=gps&output=json&key={$key}&locations={$turn}";
			// dump($url);
			$json = file_get_contents($url);
			Log::record($json,'error');
			$arr = json_decode($json,true);
			// dump($arr);
			// die('ok');
			if($arr['infocode'] == '10000'){
				$save = $arr['locations'];
				$turn_gps = explode(',',$save);
				$longitude = $turn_gps[0];
				$latitude = $turn_gps[1];

			}else{
				Log::record($json,'error');
				$longitude = $out['longitude'];
				$latitude = $out['latitude'];
				
			}
			$out['longitude'] = $longitude;
			$out['latitude'] = $latitude;

			return $out;
			
	}